package com.poorstu.manager.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.poorstu.dao.FlowDao;
import com.poorstu.dao.LoadDao;
import com.poorstu.dao.LoginDao;
import com.poorstu.dao.PoorStudentDao;
import com.poorstu.manager.PoorStudentManager;
import com.poorstu.model.AuditLoadMod;
import com.poorstu.model.AuditsMod;
import com.poorstu.model.ClassMod;
import com.poorstu.model.FlowLineMod;
import com.poorstu.model.FlowMod;
import com.poorstu.model.LoadMod;
import com.poorstu.model.LoginMod;
import com.poorstu.model.StuPoorMod;

public class PoorStudentManagerImpl  implements PoorStudentManager {
	private	PoorStudentDao poorStudentDao;
	private	FlowDao flowDao;
	private	LoginDao loginDao;
	private	LoadDao loadDao;
	
	public List<StuPoorMod> selectStuPoorByAllUserId(int allUserId) {
		
	List<AuditsMod> auditList=poorStudentDao.selectStuPoor(allUserId);
	List<StuPoorMod> stuPoorList=new ArrayList<StuPoorMod>();
	for(int i=0;i<auditList.size();i++)
	{
	AuditsMod auditMod=new  AuditsMod();
	StuPoorMod stuPoor=	poorStudentDao.selectPoor(auditList.get(i).getAuditId());
	auditMod.setAudStatus(stuPoor.getAudits().getAudStatus());
	stuPoor.setAppDate(stuPoor.getAppDate());
	System.out.print("time"+stuPoor.getAppDate());
	stuPoorList.add(stuPoor);
	
	}
		return stuPoorList;
	}
	
	
	public void poorStuAdd(int allUserId ,String poorApplyer,String poorLevel)
	{
		int flowId=2;
		FlowMod flow=flowDao.selcetFlowById(flowId);
		LoginMod login=loginDao.selectUserById(allUserId);
		AuditsMod auditMod=new  AuditsMod();
		auditMod.setAudStatus(0);
		auditMod.setFlow(flow);
		auditMod.setLogins(login);
		auditMod.setTimes(new Date());
		loadDao.saveAudit(auditMod);
		
		StuPoorMod stuPoor=new StuPoorMod();
		stuPoor.setAppDate(new Date());
		stuPoor.setApplyre(poorApplyer);
		stuPoor.setAudits(auditMod);
		
		stuPoor.setLevels(poorLevel);
		poorStudentDao.poorStuAdd(stuPoor);
		
		
	}
	
	
	
	
	public void deletePoorStu(int poorId) {
		StuPoorMod stuPoor=poorStudentDao.selectStuPoorById(poorId);
		poorStudentDao.DeletePoorStu(stuPoor);
		
	}
	
	
	
	


	public List<StuPoorMod> AuditPoorStu(int roleId, int allUserId) {

       List<StuPoorMod> poorList=poorStudentDao.selectStuPoorAll();
		
		LoginMod login=loginDao.selectUserById(allUserId);
		int userClassId=login.getClassId().getClassId();
		int userCollegeId=login.getCollege().getCollegeId();
		
		List<StuPoorMod> stuPoorList=new ArrayList<StuPoorMod>();
		for(int i=0;i<poorList.size();i++){
			int status=	poorList.get(i).getAudits().getAudStatus();
			int classId=poorList.get(i).getAudits().getLogins().getClassId().getClassId();
			//当用户不属于莫一个班级是 但一定属于某个学院
			int usercollege=poorList.get(i).getAudits().getLogins().getCollege().getCollegeId();
			
			//int cllegeId=loadList.get(i).getAuditss().getLogins().getClassId().getCollegeId().getCollegeId();
			List<FlowLineMod> flowLineList=poorList.get(i).getAudits().getFlow().getFlowLineList();
			for(int j=0;j<flowLineList.size();j++)
			{
				if(userClassId==0&&status==flowLineList.get(j).getOrders()&&flowLineList.get(j).getRole().getRoleId()==roleId&&userCollegeId==usercollege){
					StuPoorMod stuPoor=new  StuPoorMod();
					LoginMod loginMod=new  LoginMod();
					ClassMod classes=new ClassMod();
					AuditsMod auditMod=new AuditsMod();
					loginMod.setName(poorList.get(i).getAudits().getLogins().getStu().getStuName());
					loginMod.setUserAccount(poorList.get(i).getAudits().getLogins().getUserAccount());
					classes.setClassName(poorList.get(i).getAudits().getLogins().getClassId().getClassName());
					loginMod.setClassId(classes);
					
					auditMod.setLogins(loginMod);
					auditMod.setAudStatus(poorList.get(i).getAudits().getAudStatus());
					
					stuPoor.setApplyre(poorList.get(i).getApplyre());
					stuPoor.setAppDate(poorList.get(i).getAppDate());
					stuPoor.setLevels(poorList.get(i).getLevels());
					stuPoor.setStupoorId(poorList.get(i).getStupoorId());
					stuPoor.setAudits(auditMod);
					stuPoorList.add(stuPoor);
					
				} 
				
				else if(status==flowLineList.get(j).getOrders()&&flowLineList.get(j).getRole().getRoleId()==roleId&&classId==userClassId&&userCollegeId==usercollege)
				{
					StuPoorMod stuPoor=new  StuPoorMod();
					LoginMod loginMod=new  LoginMod();
					ClassMod classes=new ClassMod();
					AuditsMod auditMod=new AuditsMod();
					loginMod.setName(poorList.get(i).getAudits().getLogins().getStu().getStuName());
					loginMod.setUserAccount(poorList.get(i).getAudits().getLogins().getUserAccount());
					classes.setClassName(poorList.get(i).getAudits().getLogins().getClassId().getClassName());
					loginMod.setClassId(classes);
					
					auditMod.setLogins(loginMod);
					auditMod.setAudStatus(poorList.get(i).getAudits().getAudStatus());
					
					stuPoor.setApplyre(poorList.get(i).getApplyre());
					stuPoor.setAppDate(poorList.get(i).getAppDate());
					stuPoor.setLevels(poorList.get(i).getLevels());
					stuPoor.setStupoorId(poorList.get(i).getStupoorId());
					stuPoor.setAudits(auditMod);
					stuPoorList.add(stuPoor);
				}
			}
		}
		return stuPoorList;
	}
	
	
	



	public void AuditPoorStuDetil(int audtiStatu, String remark, int poorId) {
		 int auditId=poorStudentDao.selectStuPoorById(poorId).getAudits().getAuditId();	
		  AuditsMod auditMod=loadDao.selectAuditById(auditId);
		  int flowId=auditMod.getFlow().getFlowId();
		  int orders=flowDao.selectOder(flowId);
		 int status=auditMod.getAudStatus();
		 
		  if(audtiStatu==0)
		  {
			  audtiStatu=99;
		  }
		  else  if(status==orders){
			  audtiStatu =100;
		  }
		  auditMod.setAudStatus(audtiStatu);
		  auditMod.setRemark(remark);
		  auditMod.setTimes(new Date());
			loadDao.updateAudit(auditMod);
			
			
		}


	public void updatePoorStu(StuPoorMod stuPoor) {
		poorStudentDao.updatePoorStu(stuPoor);
	}


	public StuPoorMod selectPootStuById(int poorStuId) {
		return poorStudentDao.selectStuPoorById(poorStuId);
	}


	public void setPoorStudentDao(PoorStudentDao poorStudentDao) {
		this.poorStudentDao = poorStudentDao;
	}



	public void setFlowDao(FlowDao flowDao) {
		this.flowDao = flowDao;
	}


	public LoginDao getLoginDao() {
		return loginDao;
	}


	public void setLoginDao(LoginDao loginDao) {
		this.loginDao = loginDao;
	}


	public void setLoadDao(LoadDao loadDao) {
		this.loadDao = loadDao;
	}
	
	
	

}
